package com.ciwong.epaper.modules.me.ui.mall;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.util.DisplayUtils;

import java.util.ArrayList;
import java.util.List;


@SuppressLint("HandlerLeak")
public class SlideShowView extends FrameLayout {
    private List<View> imageViewsList;
    private List<View> dotViewsList;
    private LinearLayout mLinearLayout;
    private ViewPager mViewPager;
    private List<Service> mServiceList = new ArrayList<>();
    private  boolean isAddData=false;

    public SlideShowView(Context context) {
        this(context, null);
    }

    public SlideShowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public SlideShowView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initUI(context);

    }

    private void initUI(Context context) {
        imageViewsList = new ArrayList<View>();
        dotViewsList = new ArrayList<View>();
        LayoutInflater.from(context).inflate(R.layout.layout_slideshow, this, true);
        mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
    }


    public void setServices(List<Service> serviceList) {

        if (!isAddData) {
            isAddData=true;

            if (serviceList != null && serviceList.size() > 0) {
                mServiceList=serviceList;
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(DisplayUtils.dip2px(getContext(), 5), 0, 0, 0);
            lp.height = DisplayUtils.dip2px(getContext(), 8);
            lp.width = DisplayUtils.dip2px(getContext(), 8);
            for (int i = 0; i < mServiceList.size(); i++) {
                ItemServiceBrief itemServiceBrief = new ItemServiceBrief(getContext());
                //update
                itemServiceBrief.update(mServiceList.get(i));

                imageViewsList.add(itemServiceBrief);
                View viewDot = new View(getContext());
                if (i == 0) {
                    viewDot.setBackgroundResource(R.drawable.dot_dark_gray);
                } else {
                    viewDot.setBackgroundResource(R.drawable.dot_light_gray);
                }
                viewDot.setLayoutParams(lp);
                dotViewsList.add(viewDot);
                mLinearLayout.addView(viewDot);
            }
            mViewPager.setFocusable(true);
            mViewPager.setAdapter(new MyPagerAdapter());
            mViewPager.setOnPageChangeListener(new MyPageChangeListener());
        }

    }


    private void setImageBackground(int selectItems) {
        for (int i = 0; i < dotViewsList.size(); i++) {
            if (i == selectItems) {
                dotViewsList.get(i).setBackgroundResource(R.drawable.dot_dark_gray);
            } else {
                dotViewsList.get(i).setBackgroundResource(R.drawable.dot_light_gray);
            }
        }
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            ((ViewPager) container).removeView(imageViewsList.get(position));
        }

        @Override
        public Object instantiateItem(View container, int position) {
            // TODO Auto-generated method stub
            ((ViewPager) container).addView(imageViewsList.get(position));
            return imageViewsList.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imageViewsList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }


        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }


    private class MyPageChangeListener implements OnPageChangeListener {

        boolean isAutoPlay = false;

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            switch (arg0) {
                case 1:
                    isAutoPlay = false;
                    break;
                case 2:
                    isAutoPlay = true;
                    break;
                case 0:
                    if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() - 1 && !isAutoPlay) {
                        mViewPager.setCurrentItem(0);
                    } else if (mViewPager.getCurrentItem() == 0 && !isAutoPlay) {
                        mViewPager.setCurrentItem(mViewPager.getAdapter().getCount() - 1);
                    }
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int pos) {
            setImageBackground(pos % mServiceList.size());
        }
    }

}



